//
//  GoodShopViewController.m
//  BTRIP
//
//  Created by johnbk on 15/11/12.
//  Copyright © 2015年 johnbk. All rights reserved.
//

#import "GoodShopViewController.h"
#import "GoodShopModel.h"
#import "GoodShopTableViewCell.h"
#import "DetailGoodShopViewController.h"
#import "DataHandle.h"
#import "CitiesViewController.h"


#import <MBProgressHUD.h>
@interface GoodShopViewController ()<UITableViewDataSource, UITableViewDelegate, CitiesViewControllerDelegate>

@property (nonatomic, retain) MBProgressHUD *hud;

@end

@implementation GoodShopViewController
-(void)dealloc{
	[_tableViewOfGoodShop release];
	[_array release];
	[_hud release];
	[_tableViewController release];
	[_buttonOfSelectedCity release];
	[super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	self.view.backgroundColor = [UIColor whiteColor];
	self.automaticallyAdjustsScrollViewInsets = NO;
	
	self.title = @"同城好店";
	self.hud = [[MBProgressHUD alloc] initWithView:self.view];
	self.selectedCity = [[NSUserDefaults standardUserDefaults] integerForKey:@"selectedCity"];
	self.array = [NSMutableArray array];
	[self createTableView];
	[self interperateData];
	[self createButtonOfSelectedCity];
	
}

- (void)createButtonOfSelectedCity{
	NSArray *arrayOfCities = @[@"北京", @"上海", @"广州", @"厦门", @"成都", @"杭州", @"深圳"];
	self.buttonOfSelectedCity = [UIButton buttonWithType:UIButtonTypeCustom];
	self.buttonOfSelectedCity.frame = CGRectMake(0, 0, 50, 30);
	
	self.buttonOfSelectedCity.titleLabel.font = [UIFont systemFontOfSize:12];
	[self.buttonOfSelectedCity setTitle:arrayOfCities[self.selectedCity] forState: UIControlStateNormal];
	[self.buttonOfSelectedCity setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
	self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.buttonOfSelectedCity];
	[self.buttonOfSelectedCity release];
	[self.buttonOfSelectedCity addTarget:self action:@selector(changeCity:) forControlEvents:UIControlEventTouchUpInside];
	
	
}
- (void)changeCity:(UIButton *)button{
	CitiesViewController *cities = [[CitiesViewController alloc]init];
	cities.delegate = self;
	[self.navigationController pushViewController:cities animated:YES];
	[cities release];
}

- (void)interperateData{
	[DataHandle contectWithURL:[NSString stringWithFormat:@"http://api.yizhong.cccwei.com/api.php?srv=2009&cid=512665&city_id=%ld&uid=0&tms=20151111172019&sig=e8af0ab4ab3ee308&wssig=5fbd1995914c148d&os_type=1&version=8&page_size=20&since_id=0", self.selectedCity + 1] andParameters:nil andViewController:self andBlock:^(id response) {
		if (response != nil) {
			[self.array removeAllObjects];
			
			NSDictionary *dictionary = response;
			NSArray *array = [[dictionary objectForKey:@"data"]objectForKey:@"items"];
			
			for (NSDictionary *dic in array) {
				GoodShopModel *goodModel = [[GoodShopModel alloc]init];
				[goodModel setValuesForKeysWithDictionary:dic];
				[self.array addObject:goodModel];
				[goodModel release];
			}
			[self.tableViewOfGoodShop reloadData];
		}
		
	}];
//	NSURLSession *session = [NSURLSession sharedSession];
//	NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.yizhong.cccwei.com/api.php?srv=2009&cid=512665&city_id=1&uid=0&tms=20151111172019&sig=e8af0ab4ab3ee308&wssig=5fbd1995914c148d&os_type=1&version=8&page_size=20&since_id=0"]];
//	NSURLSessionTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//		if (!error) {
//			NSLog(@"请求数据成功");
//			NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//			NSArray *array = [[dictionary objectForKey:@"data"]objectForKey:@"items"];
//			
//			for (NSDictionary *dic in array) {
//				GoodShopModel *goodModel = [[GoodShopModel alloc]init];
//				[goodModel setValuesForKeysWithDictionary:dic];
//				[self.array addObject:goodModel];
//				[goodModel release];
//			}
//			[self.tableViewOfGoodShop reloadData];
//			//[MBHudTool dissmissHUD:self.hud view:self.view];
//			
//	}
//	}];
//	//[MBHudTool showHUD:nil view:self.view hud:self.hud];
//	[dataTask resume];
}
- (void)createTableView{
	self.tableViewOfGoodShop = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
	[self.view addSubview:self.tableViewOfGoodShop];
	[self.tableViewOfGoodShop release];
	self.tableViewOfGoodShop.delegate = self;
	self.tableViewOfGoodShop.dataSource = self;
	
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	GoodShopTableViewCell *cell = [GoodShopTableViewCell tableView:tableView];
	cell.goodShopModel = self.array[indexPath.row];
	return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	return self.array.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
	return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return 275;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	DetailGoodShopViewController *detail = [[DetailGoodShopViewController alloc]init];
	detail.goodShopModel = self.array[indexPath.row];
	[self.navigationController pushViewController:detail animated:YES];
	[detail release];
}
- (void)currentCity:(NSString *)city{
	[self.buttonOfSelectedCity setTitle:city forState:UIControlStateNormal];
	self.selectedCity = [[NSUserDefaults standardUserDefaults]integerForKey:@"selectedCity"];
	[self interperateData];

}
- (void)viewWillAppear:(BOOL)animated{
	[self.navigationController.navigationBar setHidden:NO];
	
	
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
